<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>作品管理</el-breadcrumb-item>
      <el-breadcrumb-item>作品评论</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入评论内容搜索"
                    v-model="searchContent"
                    clearable
                    @clear="getWorkComments"
          >
            <el-button slot="append" icon="el-icon-search" @click="getWorkComments"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addWorkComment">添加评论</el-button>
        </el-col>
      </el-row>

      <el-table :data="workCommentList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="评论内容" prop="content"></el-table-column>
        <el-table-column label="作品名" prop="worksInfo.workName"></el-table-column>
        <el-table-column label="评论时间" prop="createdTime"></el-table-column>
        <el-table-column label="点赞数" prop="contentPraise"></el-table-column>
        <el-table-column label="评论状态" prop="contentStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.contentStatus"
              :inactive-value=0
              :active-value=1
              @change="changeWorkCommentStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" style="display: inline-block;" width="160px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改评论" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                         @click="showEditDialog(scope.row.commentId)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除评论" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" @click="deleteWorkComment(scope.row.commentId)"
                         size="mini"></el-button>
            </el-tooltip>

          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加评论"
      :visible.sync="addDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="addDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="评论用户" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入内容"
            v-model="addForm.content">
          </el-input>
        </el-form-item>
        <el-form-item label="评论作品" prop="workName">
          <el-select v-model="addForm.workName" placeholder="请选择" :change="changeWorkName(addForm.workName)" clearable>
            <el-option
              v-for="item in options"
              :key="item.workId"
              :label="item.workName"
              :value="item.workId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitAddWorkComment">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="修改评论"
      :visible.sync="editDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="editDialogClosed">
      <el-form :model="editForm" label-width="70px">
        <el-form-item label="评论人" prop="username">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <el-input v-model="editForm.content"></el-input>
        </el-form-item>
        <el-form-item label="作品名" prop="worksInfo.demands">
          <el-input v-model="editForm.worksInfo.workName" disabled></el-input>
        </el-form-item>
        <el-form-item label="点赞数" prop="contentPraise">
          <el-input v-model="editForm.contentPraise"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="changeWorkComment(editForm)">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
    import {
        adminAddWorkComment,
        changeWorkCommentById, deleteWorkCommentById,
        getAllWorkCommentsByPage,
        selectCommentById
    } from "../../assets/api/workComment";
    import {selectAllWorks} from "../../assets/api/worksInfoApi";

    export default {
        name: 'workComments',
        data() {
            return {
                workCommentList: [],
                searchContent: '',
                pageNum: 1,
                pageSize: 5,
                total: 0,
                addDialogVisible: false,
                addForm: {username: '', content: '', workName: '', workId: 0},
                addFormRules: {
                    username: {required: true, message: '请输入用户名', trigger: 'blur'},
                    content: {required: true, message: '请输入评论内容', trigger: 'blur'},
                    workName: {required: true, message: '请输入评论作品', trigger: 'blur'},
                },
                options: [],
                editDialogVisible: false,
                editForm: {worksInfo: {workName: ''}},
            }
        },
        methods: {
            getWorkComments() {
                let data = {};
                data.content = this.searchContent;
                getAllWorkCommentsByPage(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code === 200) {
                        this.workCommentList = res.data.records;
                        this.total = res.data.total;
                    }

                })
            },
            deleteWorkComment(commentId) {
                const confirmResult = this.$confirm(
                    '此操作将永久删除该评论, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                    let data = {};
                    data.commentId = commentId;
                    deleteWorkCommentById(data).then((res) => {
                        if (res.code === 200) {
                            this.$message.success(res.message);
                            this.getWorkComments();
                        } else {
                            this.$message.error(res.message);
                        }

                    })
                }).catch(() => {
                    this.$message.info('已取消删除');
                });

            },
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getWorkComments();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getWorkComments();
            },
            changeWorkCommentStatus(workComment) {
                changeWorkCommentById(workComment).then((res) => {
                    if (res.code === 200) {
                        this.$message.success(res.message);
                    } else {
                        this.$message.error(res.message);
                    }
                })
            },
            changeWorkName(workId) {
                this.addForm.workId = workId;
            },
            addWorkComment() {
                this.addDialogVisible = true;
                this.getWorkName();
            },
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
            },
            submitAddWorkComment() {
                this.$refs.addFormRef.validate(valid => {
                    if (!valid) return;
                    adminAddWorkComment(this.addForm).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error(res.message);
                        } else {
                            this.$message.success(res.message);
                            this.addDialogVisible = false;
                            this.getWorkComments();
                        }

                    })
                })
            },
            showEditDialog(commentId) {
                this.editDialogVisible = true;
                let data = {};
                data.commentId = commentId;
                selectCommentById(data).then((res) => {
                    this.editForm = res.data;
                })
            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
            },
            getWorkName() {
                selectAllWorks().then((res) => {
                    this.options = res.data;
                })
            },
            changeWorkComment() {
                changeWorkCommentById(this.editForm).then((res) => {
                    if (res.code !== 200) {
                        this.$message.error(res.message);
                    }
                    this.editDialogVisible = false;
                    this.getWorkComments();
                    this.$message.success(res.message);
                })
            },
        },
        created() {
            this.getWorkComments();
        }
    }
</script>

<style scoped>
  @import "../../styles/css/global.css";

</style>
